//
//  CYMyNoticeCellAutoHeight.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/8/16.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYMyNoticeCellHeight.h"

@implementation CYMyNoticeCellHeight
//根据data,动态获取高度，我的消息页面，通知，公告，活动
+ (NSArray *)autoHeight:(NSMutableArray *)data{
    if (!data)
        return nil;
    //    CGFloat offset_height = AL(PT(25)) * 3 + AL(PT(16)) * 2;//固定的间隔高度；
    CGFloat IMG_height = 178 / 754.0 *(SCREEN_WIDTH - 2 * AL(PT(25)));//活动图片的预设高度
    
    CGFloat title_size = AL(PT(30));//标题字体大小
    CGFloat content_size = AL(PT(26));//内容字体大小
    CGFloat date_size = AL(PT(22));//日期字体
    
    NSDictionary *title_attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:title_size weight:UIFontWeightBold]};
    NSDictionary *content_attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:content_size]};
    NSDictionary *date_attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:date_size]};
    
    NSMutableArray *array = [NSMutableArray array];
    
    for (int i = 0; i < data.count; i ++) {
        
        NSString *title = (data[i][@"title"])?data[i][@"title"]:[data[i][@"active_title"] substringWithRange:NSMakeRange(0, 2)];//标题，如果是活动的标题，则只取一行标题的高度，所以取两个字的高度
        NSString *content = (data[i][@"contents"])?data[i][@"contents"]:(data[i][@"content"])?data[i][@"content"]:[data[i][@"active_description"] substringWithRange:NSMakeRange(0, 2)];//内容，如果是活动的内容，则只取一行内容的高度，所以取两个字的高度
        NSString *dateStr = (data[i][@"create_time"])?data[i][@"create_time"]:data[i][@"time"];//日期
        
        CGFloat offset_height = ((data[i][@"content"])?([[data[i] allKeys] count] - 1):[[data[i] allKeys] count]) * AL(PT(25)) + AL(PT(16)) * 2;//固定的间隔高度；
        CGFloat title_height = [self getHeight:title with:title_attribute];
        CGFloat content_height = [self getHeight:content with:content_attribute];
        CGFloat date_height = ceil([dateStr sizeWithAttributes:date_attribute].height);
        if (data[i][@"mobile_img_url"] != [NSNull null] && [data[i][@"mobile_img_url"] length] > 0) //如果图片地址有值，则代表是活动
            [array addObject:@(ceil(offset_height + title_height + content_height + IMG_height))];
        else
            [array addObject:@(ceil(offset_height + title_height + content_height + date_height))];
    }
    return array;
}

+ (CGFloat)getHeight:(NSString *)text with:(NSDictionary *)attribute{
    return ceil([text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - AL(PT(25)) * 2, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine attributes:attribute context:nil].size.height);
}

@end
